package com.softstao.guoyu.ui.activity.sale;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.model.sale.FromOrder;
import com.softstao.guoyu.model.sale.OrderDetail;
import com.softstao.guoyu.model.sale.OrderGoods;
import com.softstao.guoyu.model.sale.ShippingCondition;
import com.softstao.guoyu.ui.adapter.OrderGoodsAdapter;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/21.
 */

public class OrderShippingActivity extends BaseActivity {
    @BindView(R.id.order_sn)
    TextView orderSn;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.orderer)
    TextView orderer;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.shipping_company)
    EditText shippingCompany;
    @BindView(R.id.shipping_num)
    EditText shippingNum;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    private OrderDetail orderDetail;
    private FromOrder fromOrder;
    private String type;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private OrderGoodsAdapter adapter;
    private ShippingCondition condition = new ShippingCondition();
    private String order_code,order_date,name,price;
    private List<OrderGoods> goodsList = new ArrayList<>();

    @Override
    public int _ContentView() {
        return R.layout.activity_order_shipping;
    }

    @Override
    public void initView() {
        initTitle("填写快递信息");
        type = getIntent().getStringExtra("type");
        switch (type){
            case "detail":
                orderDetail = (OrderDetail) getIntent().getSerializableExtra("order_detail");
                order_code = orderDetail.getOrderCode();
                order_date = format.format(new Date(orderDetail.getOrderDate()));
                name = orderDetail.getConsigneeInfo().getName();
                price = getResources().getString(R.string.rmb) + LZUtils.priceFormat(orderDetail.getTotal());
                goodsList.clear();
                goodsList.addAll(orderDetail.getOrderProductList());
                break;
            case "from":
                fromOrder = (FromOrder) getIntent().getSerializableExtra("order_detail");
                order_code = String.valueOf(fromOrder.getOrderCode());
                order_date = format.format(new Date(fromOrder.getOrderDate()));
                name = fromOrder.getFormAgentName();
                price = getResources().getString(R.string.rmb) + LZUtils.priceFormat(fromOrder.getTotal());
                goodsList.clear();
                goodsList.addAll(fromOrder.getOrderProductList());
                break;
        }
        orderSn.setText("订单编号：" + order_code);
        time.setText("时间：" + order_date);
        orderer.setText("订货人：" + name);
        totalPrice.setText(price);

        adapter = new OrderGoodsAdapter(goodsList);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));
    }

    @OnClick(R.id.submit_btn)
    public void onViewClicked() {
        if (!check()){
            return;
        }
        condition.setMode(shippingCompany.getText().toString());
        condition.setLogisticsCode(shippingNum.getText().toString());
        Intent intent = getIntent();
        intent.putExtra("shipping",condition);
        setResult(RESULT_OK,intent);
        finish();
    }

    private boolean check(){
        if (TextUtils.isEmpty(shippingCompany.getText().toString())){
            LZToast.getInstance(context).showToast("请填写快递公司");
            return false;
        }
        if (TextUtils.isEmpty(shippingNum.getText().toString())) {
            LZToast.getInstance(context).showToast("请填写物流单号");
            return false;
        }
        return true;
    }
}
